package com.cxf.cxfdaybat.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 * description: DispatchTaskController <br>
 * date: 2021/11/13 23:39 <br>
 * author: cxf <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/api/daybat")
public class DispatchTaskController {

       private Logger logger = LoggerFactory.getLogger(DispatchTaskController.class);
        @Autowired
        @Qualifier("personJobLauncher")
        SimpleJobLauncher simpleJobLauncher;

        @Autowired
        @Qualifier("personJob")
        Job importJob;


        @GetMapping("/personTask")
        public String personTask(){

        // 后置参数：使用JobParameters中绑定参数
        JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                .toJobParameters();
        try {
            JobExecution jobExecution = simpleJobLauncher.run(importJob, jobParameters);
            simpleJobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());

            BatchStatus status = jobExecution.getStatus();

            logger.info("I GET RESULT status: >>>>>>>> "+  status);

            // COMPLETED 成功
            // FAILED  失败

        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }

        return "成功";

    }



}
